<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="200px" style="width: 600px">
      <el-form-item
        label="业务线"
        prop="business_id"
        :rules="[
          { required: true, message: '业务线不能为空'}
        ]"
      >
        <el-select v-model="form.business_id" filterable placeholder="请选择" @change="fetchProject()">
          <el-option
            v-for="item in businesslist"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        ref="project"
        label="项目"
        prop="project_id"
        :rules="[
          { required: true, message: '项目不能为空'}
        ]"
      >
        <el-select v-model="form.project_id" filterable placeholder="请选择" @change="fetchService()">
          <el-option
            v-for="item in projectlist"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        ref="service"
        label="服务"
        prop="service_id"
        :rules="[
          { required: true, message: '服务不能为空'}
        ]"
      >
        <el-select v-model="form.service_id" filterable placeholder="请选择">
          <el-option
            v-for="item in servicelist"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="工单类型"
        prop="order_type"
        :rules="[
          { required: true, message: '类型不能为空'}
        ]"
      >
        <el-radio-group v-model="form.order_type" size="small">
          <!-- <el-radio-button label="deploy" checked>更新</el-radio-button> -->
          <el-radio label="restart">重启</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item v-if="form.type === 'deploy'" label="更新环境">
        <el-checkbox-group v-model="form.env">
          <el-checkbox
            v-for="item in envlist"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item v-if="form.type === 'deploy'" label="更新类别">
        <el-radio-group v-model="form.updatetype">
          <el-radio
            v-for="item in updatetypelist"
            :key="item.value"
            :label="item.key"
            :value="item.value"
          />
        </el-radio-group>
      </el-form-item> -->
      <el-form-item
        label="重启环境"
        prop="env_id"
        :rules="[
          { required: true, message: '环境不能为空'}
        ]"
      >
        <el-radio-group v-model="form.env_id">
          <el-radio
            v-for="item in envlist"
            :key="item.id"
            :label="item.id"
            :value="item.id"
          >{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="comment">
        <el-input v-model="form.comment" type="textarea" :autosize="{ minRows: 4, maxRows: 10}" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRowHandle('form')">立即创建</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getSelect as getBusiness } from '@/api/resource/business'
import { getSelect as getProject } from '@/api/resource/project'
import { getSelect as getService } from '@/api/resource/service'
import { getList as getEnv } from '@/api/resource/env'
import { addRow } from '@/api/flow/restart'
export default {
  data() {
    return {
      form: {
        business_id: '',
        project_id: '',
        service_id: '',
        delivery: false,
        comment: '',
        env_id: '',
        order_type: 'restart'
      },
      businesslist: [],
      projectlist: [],
      servicelist: [],
      envlist: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchBusiness() {
      const params = { business_id: this.form.business_id, page_size: 20 }
      getBusiness(params).then(response => {
        this.businesslist = response.data
      })
    },
    fetchProject() {
      // console.log(this.$refs.project)
      // this.$refs.project.resetField
      // this.$refs.service.resetField
      this.form.project_id = ''
      this.form.service_id = ''
      const params = { business: this.form.business_id, page_size: 100 }
      getProject(params).then(response => {
        this.projectlist = response.data
      })
    },
    fetchService() {
      // this.$refs.service.resetField
      this.form.service_id = ''
      const params = { business: this.form.business_id, project: this.form.project_id, page_size: 20 }
      getService(params).then(response => {
        this.servicelist = response.data
      })
    },
    fetchEnv() {
      getEnv().then(response => {
        this.envlist = response.data
      })
    },
    addRowHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          addRow(this.form).then(response => {
            const flowid = response.id

            this.$message({
              type: 'success',
              message: '创建成功,即将跳转至详情页',
              duration: 1200,
              onClose: () => {
                console.log(flowid)
                this.$router.push({ 'name': 'RestartDetail', 'query': { id: flowid }})
              }
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    fetchData() {
      this.fetchBusiness()
      this.fetchEnv()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
